<?php

class BioCASeLoad {
    var $db;
    var $config;
    var $CollectionObjectID;
    
    public function __construct($db, $collectionobjectid) {
        $this->db = $db;
        $this->CollectionObjectID = $collectionobjectid;
        $this->config = array();
        $this->config();
    }
    
    private function config() {
        $this->config['unit'] = (object) array(
            'tablename' => 'unit',
            'type' => 'delete and insert',
            'skipprimary' => FALSE,
            'prefix' => 'abcd'
        );
        $this->config['identification'] = (object) array(
            'tablename' => 'identification',
            'type' => 'delete and insert',
            'skipprimary' => FALSE,
            'prefix' => 'abcd'

        );
        $this->config['abcd_highertaxon'] = (object) array(
            'tablename' => 'higher_taxon',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'abcd'

        );
        $this->config['collector'] = (object) array(
            'tablename' => 'collector',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'abcd'

        );
        $this->config['previousunit'] = (object) array(
            'tablename' => 'previous_unit',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'abcd'

        );
        $this->config['unitmeasurementorfact'] = (object) array(
            'tablename' => 'unitmeasurementorfact',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'abcd'

        );
        $this->config['namedarea'] = (object) array(
            'tablename' => 'named_area',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'abcd'
        );
        $this->config['taxonname'] = (object) array(
            'tablename' => 'taxonname',
            'type' => 'delete and insert',
            'skipprimary' => FALSE,
            'prefix' => 'aux'
        );
        $this->config['aux_highertaxon'] = (object) array(
            'tablename' => 'highertaxon',
            'type' => 'delete and insert',
            'skipprimary' => TRUE,
            'prefix' => 'aux'
        );
        
        
    }
    
    public function load($dbtable, $data, $reindex=FALSE) {
        $config = $this->config[$dbtable];

        if (is_array($data)) {
            if ($config->type == 'delete and insert' && !$reindex) {
                $delete = "DELETE FROM biocase.{$config->prefix}_{$config->tablename}
                    WHERE collection_object_id=$this->CollectionObjectID";
                $stmt = $this->db->prepare($delete);
                if (!$stmt->execute()) {
                    $this->errorInfo($stmt, $config);
                }
            }
            foreach ($data as $item)
                $this->load ($dbtable, $item);
        }
        elseif (is_object($data)) {
            $fields = array_keys((array) $data);
            if ($config->skipprimary)
                array_shift ($fields);
            
            $values = array();
            foreach ($fields as $key=>$field) {
                $values[] = '?';
                $fields[$key] = trim(strtolower(preg_replace('/([A-Z]+)([a-z0-9]+)/', '$1$2_', $field)), '_');
            }
            $fields = implode(', ', $fields);
            $values = implode(', ', $values);
            
            if ($config->type == 'replace' && !$reindex) {
                $sql = "REPLACE INTO biocase.{$config->prefix}_{$config->tablename} ($fields)
                    VALUES($values)";
            }
            else {
                if ($config->tablename == 'unit') {
                    $delete = "DELETE FROM biocase.{$config->prefix}_{$config->tablename}
                        WHERE collection_object_id=$this->CollectionObjectID";
                    $stmt = $this->db->prepare($delete);
                    if (!$stmt->execute()) {
                        $this->errorInfo($stmt, $config);
                    }
                }
                $sql = "INSERT INTO biocase.{$config->prefix}_{$config->tablename} ($fields)
                    VALUES($values)";
            }
            
            $stmt = $this->db->prepare($sql);
            
            $values = array_values((array) $data);
            if ($config->skipprimary)
                array_shift ($values);
            
            if (!$stmt->execute($values)) {
                $this->errorInfo($stmt, $config);
            }
            
        }
            
    }
    
    private function errorInfo($stmt, $config) {
        $errorinfo = $stmt->errorInfo();

        $sql = "INSERT INTO biocase.import_errors (collection_object_id, table_name, sqlstate_error_code, error_message, timestamp_created)
            VALUES (?, ?, ?, ?, ?)";
        $ins = $this->db->prepare($sql);

        $timestamp = date('Y-m-d H:i:s');
        $error = array(
            $this->CollectionObjectID,
            $config->tablename,
            $errorinfo[0],
            $errorinfo[2],
            $timestamp
        );
        $ins->execute($error);
    }
}
?>
